package com.youjp.letcode.let01;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @PackageName: com.youjp.letcode.let01
 * @author: youjp
 * @slogan: 天下风云出我辈，一入代码岁月催
 * @create: 2023-10-30 20:51
 * @description:
 * @Version: 1.0
 */
public class Solution2 {
    public int[] twoSum(int[] nums, int target) {
        // 使用map集合
        Map<Integer,Integer> map = new HashMap<Integer,Integer>(nums.length-1);
        for (int i = 0; i < nums.length; i++) {
            int tmp = target -nums[i];
            if (map.containsKey(tmp)){
                return new int[]{i,map.get(tmp)};
            }
            //存储值
            map.put(nums[i],i);
        }

        throw new IllegalArgumentException("No two sum solution");
    }

    public static void main(String[] args) {
        int nums[] ={2,7,11,15};
        int target=9;
        System.out.println(Arrays.toString(new Solution2().twoSum(nums,target)));
    }
}
